;/*
	 * 该类主要实现单点登录功能，过渡页面
	 */
cmedit.LoginBycas = (function($, window, document) {
	var thisContext;
	var loginBycasFn;
	var userId;//用户userID
	var token;//用户token
	var userName;//用户名
	var tenantId;//用户头像地址
	var cloudType;//当前系统模式(true:公有云;false:私有云)
	var isPublish = cmedit.global.isPublish;
	
	
	var LoginBycasClass = function() {
		window.sessionStorage.setItem("dyportalServerPath",cmedit.global.portalPath);
		thisContext = this;
		thisContext.loginBycasFn = function(){};
	};
	LoginBycasClass.prototype = {
		//构造函数
		constructor : LoginBycasClass,
	    init : function() {
			thisContext.initViewStyle();
			thisContext.loginByCas();
		},
		/**
		 * 初始化页面事件
		 */
		initEvent : function() {
			// 设置portal相关js
			loginBycasFn = thisContext.loginByCas;
			var systsemcmedit = cmedit.global.getAppByCode(cmedit.global.system_cmedit_id)
			// 查询版本信息
			$.ajax({
				async : false,
				url : cmedit.global.cmsPath + "UserInfoController/getModuleInfo",
				type : 'GET',
				success : function(data) {
					if (data.status == true || data.status == 'true') {
						$.initHeader({
							"divId": "CMEDIT_portal",                          //头部存放区域id
							"highLight": systsemcmedit.appGuid,   //高亮应用id
							 "aboutInfo": [{      //需要在导航栏中关于模块要展现的信息，无属性则不展现
	                             "aboutName":"版本信息",
	                             "aboutCode":"Vesion",
	                             "aboutValue":data.data.version
	                            },
								{      //需要在导航栏中关于模块要展现的信息
								  "aboutName":"升级时间",
								  "aboutCode":"UpdateTime",
								  "aboutValue":data.data.updateTime
								}
								]
						},loginBycasFn);
					} else {
						cmedit.global.alertMsgNoCancel(data.msg);
					}
				}
			});
		},
		/**
		 * 初始化页面视图
		 */
		initViewStyle : function() {

		},
		
		/**
		 * 单点登录
		 * @param casData
		 */
		loginByCas : function(casData) {
			//清除外系统标识缓存
            window.sessionStorage.removeItem(cmedit.global.externalSystemCode);
            
            var pass_dog = false;
        	//验证加密狗是否通过：true通过，false不通过
        	$.ajax({
        		type : 'POST',
				async : false,
				url : cmedit.global.cmsPath + 'UserInfoController/checkDog',
				dataType : 'json',
				success: function (data) {
					pass_dog=data.data;
				}
        	});
            
        	if(pass_dog == true || pass_dog == "true"){
    			userId = '0003';
    			token = 'biubiubiu';
    			userName = '0003';
    			tenantId = 'dayang';
    			cloudType  = false;
        		
    			

    			if (userId == null || userId == "" || userId == undefined) {
    				alert("userId为空！")
    				return;
    			}
    			if (token == null || token == "" || token == undefined) {
    				alert("token为空")
    				return;
    			}
    			if (userName == null || userName == "" || userName == undefined) {
    				alert("userName为空！")
    				return;
    			}
    			if (tenantId == null || tenantId == "" || tenantId == undefined) {
    				alert("tenantId为空！")
    				return;
    			}
    			//console.log("单点登录成功")
				//window.location.href = 'views/manuscript/manuscript_list.html';
    			$.ajax({
					type : 'POST',
					async : true,
					url : cmedit.global.cmsPath + 'UserInfoController/loginBycas',
					dataType : 'json',
					data : {
						cloudType : cloudType,
						key : '0003'
					},
					beforeSend: function (XHR) {
	                    XHR.setRequestHeader("userId", userId);
	                    XHR.setRequestHeader("tenantId", tenantId);
	                    XHR.setRequestHeader("token", token);
					},
					success : function(data) {
						if (data.status == true) {
							window.localStorage.setItem("portalKey","0003");
							window.sessionStorage.setItem(cmedit.global.userInfo, JSON.stringify(data.data));
							var privilegeList = data.data.privilegeList;
							var bigColumnId = data.data.bigColumnId;
							var privilegeIds = [];
							var userColumnIds = [];
							var allColumnIds = [];
							var columnList = data.data.columnList;
							var allColumnList = data.data.allColumnList;
							var CFBFlag = data.data.CFBFlag;
							var CREFlag = data.data.CREFlag;
							var rongZhiLianFlag = data.data.rongZhiLianFlag;
							var rongYunFlag = data.data.rongYunFlag;
							var creImportTaskFlag = data.data.creImportTaskFlag;
							var CFBExternalUrl = data.data.CFBExternalUrl;
							window.sessionStorage.setItem(cmedit.global.privilegeInfo, JSON.stringify(privilegeList));
							window.sessionStorage.setItem(cmedit.global.columnInfo, JSON.stringify(columnList));
							window.sessionStorage.setItem(cmedit.global.allColumnInfo,JSON.stringify(allColumnList));
							for(var i = 0;i<privilegeList.length;i++ ){
								privilegeIds.push(privilegeList[i].id);
							}
							for(var i = 0;i<columnList.length;i++ ){
								userColumnIds.push(columnList[i].id);
							}
							for(var i = 0;i<allColumnList.length;i++){
								allColumnIds.push(allColumnList[i].id);
							}
							//将权限id集合，栏目id集合放入sessionStorage
							window.sessionStorage.setItem(cmedit.global.privilegeIds, JSON.stringify(privilegeIds));
							window.sessionStorage.setItem(cmedit.global.userColumnIds, JSON.stringify(userColumnIds));
							window.sessionStorage.setItem(cmedit.global.allColumnIds,JSON.stringify(allColumnIds));
							window.sessionStorage.setItem(cmedit.global.CFBFlag, CFBFlag);
							window.sessionStorage.setItem(cmedit.global.CREFlag, CREFlag);
							window.sessionStorage.setItem(cmedit.global.rongZhiLianFlag, rongZhiLianFlag);
							window.sessionStorage.setItem(cmedit.global.rongYunFlag, rongYunFlag);
							window.sessionStorage.setItem(cmedit.global.creImportTaskFlag, creImportTaskFlag);
							window.sessionStorage.setItem(cmedit.global.CFBExternalUrl, CFBExternalUrl);
							window.sessionStorage.setItem(cmedit.global.bigColumnId,bigColumnId);
							console.log("单点登录成功");
							if(privilegeIds.indexOf("PID_CMEDIT_FINALCENSOR") > -1){
								window.location.href = 'views/manuscript/manuscript_list_finalcensor.html';
							}else{
								window.location.href = 'views/manuscript/manuscript_list.html';
							}
						} else {
							alert(data.msg);
						}
					}
				});
        	}else{//加密狗验证不通过
        		//alert("未检测到加密点，请联系管理员！");
        		$.warningNoDog();
        	}
		},
		/**
		 * 根据userid获取用户详情
		 */
		selectUserInfoByUserId : function() {
			$.ajax({
				async : false,
				url : cmedit.global.cmsPath
						+ "UserInfoController/getUserInfoByUserId",
				type : 'POST',
				dataType : 'json',
				data:{"key":"0003"},
				beforeSend: function (XHR) {
	                    XHR.setRequestHeader("userId", userId);
	                    XHR.setRequestHeader("tenantId", tenantId);
	                    XHR.setRequestHeader("token", token);
	            },
				success : function(data) {
					if (data.status == true || data.status == 'true') {
						var userInfoDetail = data.data[0];
						if(cmedit.global.isNotEmpty(userInfoDetail)){
							window.sessionStorage.setItem(cmedit.global.userInfoDetail,JSON.stringify(userInfoDetail));
						}else{
							cmedit.global.forceLogout("用户信息为空！");
							return;
						}
					} else {
						alert(data.msg);
					}
				}
			});
		},
		/**
		 * 用户栏目的获取
		 */
		selectUserColumnByUserId : function() {
			
			$.ajax({
				async : false,
				url : cmedit.global.cmsPath
						+ "UserInfoController/getUserColumnByUserId",
				type : 'POST',
				dataType : 'json',
				data:{"key":"0003"},
				beforeSend: function (XHR) {
                    XHR.setRequestHeader("userId", userId);
                    XHR.setRequestHeader("tenantId", tenantId);
                    XHR.setRequestHeader("token", token);
				},
				success : function(data) {
					if (data.status == true || data.status == 'true') {
						window.sessionStorage.setItem(cmedit.global.columnInfo,JSON.stringify(data.data));
					} else {
						alert(data.msg);
					}
				}
			});
		},
		
		/**
		 * 用户权限的获取
		 */
		selectUserPrivilegeByUserId : function() {
			$.ajax({
				async : false,
				url : cmedit.global.cmsPath
				+ "UserInfoController/getPrivilegeByUserId",
				type : 'POST',
				dataType : 'json',
				data:{"key":"0003"},
				beforeSend: function (XHR) {
					XHR.setRequestHeader("userId", userId);
					XHR.setRequestHeader("tenantId", tenantId);
					XHR.setRequestHeader("token", token);
				},
				success : function(data) {
					if (data.status == true || data.status == 'true') {
						window.sessionStorage.setItem(cmedit.global.privilegeInfo,JSON.stringify(data.data));
					} else {
						alert(data.msg);
					}
				}
			});
		},
		
	};
	return new LoginBycasClass();
})(jQuery, window, document);

//优先动态加载css和js后执行页面初始化方法
$.getScript(cmedit.global.getPortalWeb() + 'static/scripts/utils/headerPlugin.min.js',function(){  //加载js,成功后，并执行回调函数  ; 
	var portal_css = cmedit.global.getPortalWeb() + 'static/css/header.min.css';
	cmedit.global.loadStyles(portal_css);
	// 页面加载完毕后初始化方法调用
	$(cmedit.LoginBycas.init());
}); 

